package common

import (
	"strconv"

	"github.com/gin-gonic/gin"
)

func NewPageRequest() *PageRequest {
	return &PageRequest{
		PageNumber: 1,
		PageSize:   10,
	}
}

type PageRequest struct {
	PageNumber int `json:"page_number"`
	PageSize   int `json:"page_size"`
}

func (p *PageRequest) Offect() int {
	return (p.PageNumber - 1) * p.PageSize
}

func (p *PageRequest) PageRequestFromGinCtx(ctx *gin.Context) *PageRequest {
	pnStr := ctx.Query("page_number")
	psStr := ctx.Query("page_size")
	if pnStr != "" {
		p.PageNumber, _ = strconv.Atoi(pnStr)
	}
	if pnStr != "" {
		p.PageSize, _ = strconv.Atoi(psStr)
	}
	return p
}
